#!/bin/sh

### BEGIN INIT INFO
# Provides:        Collision Avoidance Daemon
# Required-Start:  $network #FIXME
# Required-Stop:   $network #FIXME
# Default-Start:   2 3 4 5
# Default-Stop:
# Description:     Enable collision avoidance capabilities.
### END INIT INFO

. /etc/init.d/functions

get_pid() {
    pidof "coav-control"
}

start() {
    nohup @CMAKE_INSTALL_FULL_BINDIR@/coav-control \
        -d DI_OBSTACLE \
        -a QC_STOP \
        -s ST_REALSENSE \
        2> /tmp/coav-control.log \
        >/dev/null &

    get_pid > /dev/null 2>&1
}

stop() {
    pid=`get_pid`
    [ -n "$pid" ] && kill -KILL $pid > /dev/null 2>&1 && wait $pid 2> /dev/null || true
}

status() {
    get_pid > /dev/null 2>&1
    echo $?
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
	echo "Usage: $0 { start | stop | restart | status }" >&2
	exit 1
	;;
esac

exit $?
